var e = require;
var t = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0});
var n = cc._decorator,
    a = n.ccclass,
    i =
        (n.property,
        (function () {
            function e() {}
            return (
                (e.showProgressBar = function (e) {
                    void 0 === e && (e = 0), this.showMask();
                }),
                (e.updateProgress = function (e, t) {
                    var o = e / t;
                    o > 1 && (o = 1);
                }),
                (e.hideProgressBar = function (e) {
                    void 0 === e && (e = 1), this.hideMask(e);
                }),
                (e.showMask = function (e) {
                    void 0 === e && (e = 1);
                }),
                (e.hideMask = function (e) {
                    void 0 === e && (e = 1);
                }),
                (e.loadBundle = function (e, t, o, n) {
                    void 0 === o && (o = !1), void 0 === n && (n = !1), this.loadSubpackage(e, t, o, n);
                }),
                (e.loadSubpackage = function (e, t, o, n) {
                    void 0 === o && (o = !1), void 0 === n && (n = !1);
                    var a = this.subpackageRecords[e];
                    switch ((a || ((a = new s(e, t, o)), (this.subpackageRecords[e] = a)), a.state)) {
                        case r.inited:
                            o && this.showSubpackageProgress(),
                                n && this.subpackageSequence.length > 0
                                    ? (this.subpackageSequence.splice(1, 0, e), a.enterSequence())
                                    : (this.subpackageSequence.push(e),
                                      this.subpackageSequence.length > 1 ? a.enterSequence() : this._loadSubpackage(e));
                            break;
                        case r.waiting:
                            if (
                                (o && this.showSubpackageProgress(),
                                a.pushCb(t, o),
                                n && this.subpackageSequence.length > 0)
                            ) {
                                var i = this.subpackageSequence.indexOf(e);
                                i > 1 &&
                                    (this.subpackageSequence.splice(i, 1),
                                    this.subpackageSequence.splice(1, 0, e),
                                    a.enterSequence());
                            }
                            break;
                        case r.turnTo:
                            o && this.showSubpackageProgress(), a.pushCb(t, o), this._loadSubpackage(e);
                            break;
                        case r.loading:
                            o && this.showSubpackageProgress(), a.pushCb(t, o);
                            break;
                        case r.finished:
                            setTimeout(function () {
                                t && t();
                            }, 0);
                    }
                }),
                (e._loadSubpackage = function (e) {
                    var t = this;
                    this.subpackageRecords[e].loadStart(),
                        cc.assetManager.loadBundle(e, function (o) {
                            if (o) return console.error("子包加载出错：", e), void console.error(o);
                            console.log("子包加载完成：", e);
                            var n = t.subpackageSequence.indexOf(e);
                            if (
                                (t.subpackageSequence.splice(n, 1),
                                t.hideSubpackageProgress(),
                                t.subpackageRecords[e].loadFinish(),
                                t.subpackageSequence.length > 0)
                            ) {
                                var a = t.subpackageSequence[0],
                                    i = t.subpackageRecords[a];
                                i && i.turnToLoad(), t.loadSubpackage(a, null, !!t.subpackageRecords[a].maskCount);
                            }
                        });
                }),
                (e.showSubpackageProgress = function () {
                    null === this.subpackageProgressTimer && (this.showProgressBar(), (this.subpackageProgress = 0));
                }),
                (e.updateSubpackageProgress = function () {
                    (this.subpackageProgress += 0.03), this.subpackageProgress >= 1 && (this.subpackageProgress = 0);
                }),
                (e.hideSubpackageProgress = function () {
                    if (null !== this.subpackageProgressTimer) {
                        for (var e = 0, t = this.subpackageSequence.length - 1; t >= 0; --t)
                            e += this.subpackageRecords[this.subpackageSequence[t]].maskCount;
                        0 == e &&
                            (clearInterval(this.subpackageProgressTimer),
                            (this.subpackageProgressTimer = null),
                            (this.subpackageProgress = 0),
                            this.hideProgressBar());
                    }
                }),
                (e.loadBundleRes = function (e, t, o, n, a) {
                    var i = this,
                        r = cc.assetManager.getBundle(e);
                    if (!r) return console.error("资源包 " + e + " 尚未加载，无法获取资源:", t), void o(null);
                    var s = null;
                    void 0 === n ? (a = !0) : "boolean" == typeof n ? (a = !!n) : ((s = n), void 0 === a && (a = !0)),
                        a && this.showMask(),
                        null !== s
                            ? r.load(t, s, function (e, t) {
                                  if ((a && i.hideMask(), e)) return console.error(e.message || e), void o(null);
                                  o(t);
                              })
                            : r.load(t, function (e, t) {
                                  if ((a && i.hideMask(), e)) return console.error(e.message || e), void o(null);
                                  o(t);
                              });
                }),
                (e.loadBundleArray = function (e, t, o, n, a) {
                    var i = this,
                        r = cc.assetManager.getBundle(e);
                    if (!r) return console.error("资源包 " + e + " 尚未加载，无法获取资源数组:", t), void o(null);
                    var s = null;
                    void 0 === n ? (a = !0) : "boolean" == typeof n ? (a = !!n) : ((s = n), void 0 === a && (a = !0)),
                        a && this.showProgressBar(),
                        s
                            ? r.load(t, s, this.updateProgress.bind(this), function (e, t) {
                                  if ((a && i.hideProgressBar(), e)) return console.error(e.message || e), void o(null);
                                  o(t);
                              })
                            : r.load(t, this.updateProgress.bind(this), function (e, t) {
                                  if ((a && i.hideProgressBar(), e)) return console.error(e.message || e), void o(null);
                                  o(t);
                              });
                }),
                (e.loadBundleDir = function (e, t, o, n, a) {
                    var i = this,
                        r = cc.assetManager.getBundle(e);
                    if (!r) return console.error("资源包 " + e + " 尚未加载，无法获取资源文件夹:", t), void o(null);
                    var s = null;
                    void 0 === n ? (a = !0) : "boolean" == typeof n ? (a = !!n) : ((s = n), void 0 === a && (a = !0)),
                        a && this.showProgressBar(),
                        s
                            ? r.loadDir(t, s, this.updateProgress.bind(this), function (e, t) {
                                  if ((a && i.hideProgressBar(), e)) return console.log(e), void o(null);
                                  o(t);
                              })
                            : r.loadDir(t, this.updateProgress.bind(this), function (e, t) {
                                  if ((a && i.hideProgressBar(), e)) return console.log(e), void o(null);
                                  o(t);
                              });
                }),
                (e.loadBundleScene = function (e, t, o, n) {
                    var a = this,
                        i = cc.assetManager.getBundle(e);
                    if (!i) return console.error("资源包 " + e + " 尚未加载，无法加载场景:", t), void o(null);
                    void 0 === n && (n = !0),
                        n && this.showProgressBar(),
                        i.loadScene(t, this.updateProgress.bind(this), function (e, t) {
                            n && a.hideProgressBar(), e ? console.error(e) : o(t);
                        });
                }),
                (e.preLoadBundleRes = function (e, t, o) {
                    var n = cc.assetManager.getBundle(e);
                    n && (void 0 === o ? n.preload(t) : n.preload(t, o));
                }),
                (e.preLoadBundleArray = function (e, t, o) {
                    var n = cc.assetManager.getBundle(e);
                    n && (void 0 === o ? n.preload(t) : n.preload(t, o));
                }),
                (e.preLoadBundleDir = function (e, t, o) {
                    var n = cc.assetManager.getBundle(e);
                    n && (void 0 === o ? n.preloadDir(t) : n.preloadDir(t, o));
                }),
                (e.preLoadBundleScene = function (e, t) {
                    var o = cc.assetManager.getBundle(e);
                    o && o.preloadScene(t);
                }),
                (e.dirAsset = {}),
                (e.singleAsset = {}),
                (e.subpackageRecords = {}),
                (e.subpackageSequence = []),
                (e.subpackageProgressTimer = null),
                (e.subpackageProgress = 0),
                __decorate([a("Loader")], e)
            );
        })());
o.default = i;
var r,
    s = (function () {
        function e(e, t, o) {
            (this.name = e),
                (this.state = r.inited),
                (this.cbs = []),
                t && this.pushCb(t),
                (this.maskCount = o ? 1 : 0);
        }
        return (
            (e.prototype.pushCb = function (e, t) {
                e && this.cbs.push(e), t && this.maskCount++;
            }),
            (e.prototype.enterSequence = function () {
                this.state = r.waiting;
            }),
            (e.prototype.loadStart = function () {
                this.state = r.loading;
            }),
            (e.prototype.loadFinish = function () {
                for (; this.cbs.length > 0; ) {
                    var e = this.cbs.shift();
                    e && e();
                }
                (this.state = r.finished), (this.maskCount = 0);
            }),
            (e.prototype.turnToLoad = function () {
                this.state = r.turnTo;
            }),
            e
        );
    })();
(function (e) {
    (e[(e.inited = 1)] = "inited"),
        (e[(e.waiting = 2)] = "waiting"),
        (e[(e.loading = 3)] = "loading"),
        (e[(e.finished = 4)] = "finished"),
        (e[(e.turnTo = 5)] = "turnTo");
})(r || (r = {}));
